package com.shenmazong.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.manage.entity.TbExamLayout;
import com.shenmazong.manage.entity.TbExamSubmit;
import com.shenmazong.manage.mapper.TbExamLayoutMapper;
import com.shenmazong.manage.service.ExamManageService;
import com.shenmazong.manage.mapper.TbExamSubmitMapper;
import com.shenmazong.util.ChooseIsNull;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ExamCheatVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *serviceImpl Qi
 */
@Service
public class TbExamSubmitServiceImpl extends ServiceImpl<TbExamSubmitMapper, TbExamSubmit>
    implements ExamManageService{

    @Autowired
    private  TbExamSubmitMapper  submitMapper;
    @Autowired
    private TbExamLayoutMapper  layoutMapper;

    @Override
    public ResultResponse saveExamLayout(TbExamLayout tbExamLayout) {
        int i = layoutMapper.insert(tbExamLayout);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(layoutMapper.selectById(tbExamLayout.getLayoutId()));
    }

    @Override
    public ResultResponse deleteExamLayout(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = submitMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamLayout(TbExamLayout tbExamLayout) {
        int i = layoutMapper.updateById(tbExamLayout);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(layoutMapper.selectById(tbExamLayout.getLayoutId()));
    }

    @Override
    public ResultResponse listExamLayout(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        QueryWrapper<TbExamLayout> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamLayout::getSchoolId,paramVo.getParamMap().get("schoolId"));

        IPage<TbExamLayout> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamLayout> selectPage = layoutMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }

    @Override
    public ResultResponse saveExamSubmit(TbExamSubmit tbExamSubmit) {
        int i = submitMapper.insert(tbExamSubmit);
        if(i<1){
            return ResultResponse.FAILED("新增失败");
        }
        return ResultResponse.SUCCESS(submitMapper.selectById(tbExamSubmit.getSubmitId()));
    }

    @Override
    public ResultResponse deleteExamSubmit(BatchChooseIdVo chooseIdVo) {
        if (ChooseIsNull.idsIsNull(chooseIdVo)){
            return ResultResponse.FAILED("未选择要删除的数据");
        }
        int i = submitMapper.deleteBatchIds(chooseIdVo.getIds());
        if(i<1){
            return ResultResponse.FAILED("删除失败");
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateExamSubmit(TbExamSubmit tbExamSubmit) {
        int i = submitMapper.updateById(tbExamSubmit);
        if(i<1){
            return ResultResponse.FAILED("修改失败");
        }
        return ResultResponse.SUCCESS(submitMapper.selectById(tbExamSubmit.getSubmitId()));
    }

    @Override
    public ResultResponse listExamSubmit(ListParamVo paramVo) {
        //非空判断
        if(paramVo.getParamMap().size()==0 || paramVo.getParamMap().get("schoolId")==null){
            return ResultResponse.FAILED("无效查询条件");
        }

        QueryWrapper<TbExamSubmit> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbExamSubmit::getSchoolId,paramVo.getParamMap().get("schoolId"));

        IPage<TbExamSubmit> page = new Page<>(paramVo.getCurrent(), paramVo.getSize());
        IPage<TbExamSubmit> selectPage = submitMapper.selectPage(page,queryWrapper);
        return ResultResponse.SUCCESS(selectPage);
    }
}




